package com.heima.crm.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import com.google.code.kaptcha.Producer;
import com.heima.crm.common.contents.CommonMessageContent;
import com.heima.crm.common.result.Result;
import com.heima.crm.common.utils.CaptchaUtils;
import com.heima.crm.common.utils.OperationUtil;
import com.heima.crm.pojos.loginVo.LoginVo;
import com.heima.crm.pojos.logindto.LoginDto;
import com.heima.crm.service.CrUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;




@Slf4j
@RestController
public class CrUserLoginController {

    @Resource
    private CrUserService crUserService;

    @Resource
    private  Producer kaptchaProducer;




    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
        log.info("用户登录{}",loginDto);
        return crUserService.login(loginDto);
    }

    /**
     * 用户登录获取验证码
     * @param
     */
    /*@GetMapping("/imgCode")
    public Result getKaptcha(){

        // 生成验证码
        String text = kaptchaProducer.createText();
        BufferedImage image = kaptchaProducer.createImage(text);
        String string = UUID.randomUUID().toString();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", baos);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 获取存储的字节数据
        byte[] imageData = baos.toByteArray();
        log.info("生成的验证码为: {}", text);
        // 将图片输出给浏览器

        log.info("UUID,{}",string);
        CaptchaUtils.set(string,text);
        return Result.success(new LoginVo(imageData,string));
    }*/

/**
 * 用户登录获取验证码
 * @param
 */
    @GetMapping("/imgCode")
    public Result getKaptcha(){
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 60, 4, 0);
        // 自定义验证码内容为四则运算方式
        captcha.setGenerator(new MathGenerator(1));
        // 重新生成code
        captcha.createCode();
        String code = captcha.getCode();
        System.out.println("算数:"+code);
        if (code==null ||  "".equals(code) ){
            return Result.error(CommonMessageContent.AUTHENTICATION_ACQUISITION_FAILED);
        }
        String string = UUID.randomUUID().toString();
        String codeString = OperationUtil.calculateExpressionA(code)+"";
        System.out.println("验证码答案:"+codeString);
        CaptchaUtils.set(string,codeString);

        byte[] imageBytes = captcha.getImageBytes();
        return Result.success(new LoginVo(imageBytes,string));
    }

}


